/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.db.connection.ConnectionFactory;
import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;

import com.us.usws.common.EpServerConstants;
import com.us.usws.model.flat.epserver.DOEpTokens;

/**
 * DAO class
 * @author robot
 */
public class EpTokensDAO {

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from ep_tokens";

	private static final String SQL_GET_ALL_EPTOKENSS = "select * from ep_tokens";

	private static final String SQL_GET_TOKEN_BY_TOKEN = "select * from ep_tokens where token=? order by heartbeat_at desc limit 0,1";

	private static final String SQL_UPDATE_USER_NO = "update ep_tokens set user_no=? where id=?";

	private static final String SQL_GET_TOKEN_BY_USERUID_TOKEN = "select * from ep_tokens where user_no=? and token=? order by heartbeat_at desc limit 0,1";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor(ConnectionFactory.getInstance().getProvider(EpServerConstants.EP_DB_CONN));

	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<DOEpTokens> getAllEpTokenss() {
		return dbe.list(SQL_GET_ALL_EPTOKENSS, DOEpTokens.class);
	}

	/**
	 * Get all list by pagination.
	 */
	public List<DOEpTokens> getEpTokenss(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_EPTOKENSS, null, start, end);
		return dbe.list(swp.getSql(), DOEpTokens.class, swp.getParams());
	}

	public DOEpTokens getEpTokens(String token) {
		return dbe.load(SQL_GET_TOKEN_BY_TOKEN, DOEpTokens.class, token);
	}

	public void updateUserNo(String userNo, long tokenId) {
		dbe.execute(SQL_UPDATE_USER_NO, userNo, tokenId);
	}

	public DOEpTokens getEpTokensByUserUidAndToken(String userUid, String token) {
		return dbe.load(SQL_GET_TOKEN_BY_USERUID_TOKEN, DOEpTokens.class, userUid, token);
	}

}